Skip to content

Return nil from Date#<=> for a NaN - #199

Open
thomasklemm wants to merge 1 commit into
ruby:masterfrom
thomasklemm:thomasklemm/cmp-nan
Open

thomasklemm wants to merge 1 commit into
ruby:masterfrom
thomasklemm:thomasklemm/cmp-nan

Conversation

@thomasklemm

Copy link
Copy Markdown

Thank you for maintaining date.

Date#<=> raises ArgumentError ("comparison of Rational with NaN failed") when the other value is Float::NAN or BigDecimal("NaN"), and Date#== raises with it, while 1 <=> Float::NAN, Rational(1) <=> Float::NAN and Time.now <=> Float::NAN return nil. cmp_gen passes the result of ajd <=> other to rb_cmpint, which turns the nil from Rational#<=> into that exception.

This returns the result unchanged. Date <=> NaN is now nil and Date == NaN is false, while Date < NaN still raises ArgumentError, as for Integer. The new test covers both NaNs, so I added bigdecimal to the development group. The suite passes on Ruby 4.0.9 and master.


Found while comparing Active Model's changed? on Rails 8.1 and main with random attribute values, where a NaN assigned to a date attribute raised (record, part of my research project Lowrail).

Date#<=> passed the result of ajd <=> other to rb_cmpint, which turns
the nil that Rational#<=> returns for a NaN into an ArgumentError, so
Date <=> Float::NAN raised and Date == Float::NAN raised with it.
Return the result unchanged, as Integer#<=> and Rational#<=> do.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant